
const _M = (function () {


    //{map转换对象}
    function strMapToObj(strMap) {
        let obj = Object.create(null);
        for (let [k, v] of strMap) {
            obj[k] = v;
        }
        return obj;
    }

    //{对象转换map}
    function objToStrMap(obj) {
        let strMap = new Map();
        for (let k of Object.keys(obj)) {
            strMap.set(k, obj[k]);
        }
        return strMap;
    }

    //{map转为json}
    function strMapToJson(strMap) {
        return JSON.stringify(strMapToObj(strMap));
    }

    //{json转为map}
    function jsonToStrMap(jsonStr) {
        return objToStrMap(JSON.parse(jsonStr));
    }


    //{用负数下标选择数组} 
    //var aa=['a','b','cd'];
    //* let arr = createArray(...aa);  
    function createArray(...elements) {
        let handler = {
            get(target, propKey, receiver) {
                let index = Number(propKey);
                if (index < 0) {
                    propKey = String(target.length + index);

                }
                return Reflect.get(target, propKey, receiver);
            }
        };

        let target = [];
        target.push(...elements);
        return new Proxy(target, handler);
    }

    /**  
     * 获取指定的URL参数值  
     * URL:http://www.quwan.com/index?name=tyler  
     * 参数：paramName URL参数  
     * 调用方法:getParam("name")  
     * 返回值:tyler  
     */
    function getParam(paramName) {
        paramValue = "", isFound = !1;
        if (window.location.search.indexOf("?") == 0 && window.location.search.indexOf("=") > 1) {
            arrSource = unescape(window.location.search).substring(1, window.location.search.length).split("&"), i = 0;
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
        }
        return paramValue == "" && (paramValue = null), paramValue
    }

    /**  
    * 获取插件里判断开始时间不得大于结束时间      2015-06-01   2015-06-02   false
    * 调用方法: temporal (开始时间,结束时间) 
    * 返回值:boolean  
    */
    function temporal(time1, time2) {       //判断时间
        var BeginTime = '';
        var EndTime = '';
        time1.split('-').forEach(function (e, i) {
            BeginTime += e;
        });
        time2.split('-').forEach(function (e, i) {
            EndTime += e;
        });

        return BeginTime <= EndTime

    }

    //异步上传图片
    function img_upload(obj, pz, callback) {

        const {
            title = '请上传图片',           //标题
            uploadUrl,                     //上传url
            imgUrl,                        //回显路径
        } = pz;


        let str = `<div class="M-img" style="width:100%;height: 100%;">
                    <span class="M-title">${title}</span>
                    </div>
                    <form>
                    <input type="file" name="file" class="M-file" style="display:none;">
                    </form>`
        $(obj).append(str)           //添加隐藏文件标签

        $(obj).find('.M-img').click(function () {               //触发事件
            $(obj).find('.M-file').click()
        })
        $(obj).find('.M-file').change(function () {              //选中图片事件

            var aa=$(obj).find('form')[0]
            console.log(aa)

            

            let formData = new FormData($(obj).find('form')[0]);           //转换为js对象
            $.ajax({
                url: uploadUrl,
                type: 'POST',
                data: formData,                                         //向后台传图片
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    $(obj).find('.M-file').val('')                   //情况选项
                    callback(null, data)
                    if (data.ok == 1) {

                        $(obj).find('.M-img').css({
                            "background-image": "url('/" + imgUrl + "/" + data.path + "')"
                        })
                        $(obj).find('.M-title').css({
                            "display": "none"
                        })
                    }
                },
                error: function (returndata) {
                    callback(returndata, null)
                }
            });
        })


    }




    let ff = {
        strMapToObj,                //{map转换对象}
        objToStrMap,                //{对象转换map}
        strMapToJson,               //{map转为json}
        jsonToStrMap,               //{json转为map}
        createArray,                //{用负数下标选择数组} 
        getParam,                  //{获取URL里参数}
        temporal,                  //{开始时间不得大于结束时间 }
        img_upload,
    }

    return ff

}())

